package org.urchr.beorganized.api.impl.xml;

import javax.servlet.http.HttpServletResponse;

import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Calendar;

import org.urchr.beorganized.api.base.BaseAPIServlet;
import org.urchr.beorganized.dao.NotesDAO;
import org.urchr.beorganized.dao.errors.GeneralException;

public class ICalExport extends BaseAPIServlet {
	
	public ICalExport() {
		skipBody = true;
	}

	@Override
	protected void doProcess() throws GeneralException {
		String token = getParameter("sharecode");
		boolean addTasks = "true".equals(getParameter("tasks"));
		try {
			Calendar calendar = NotesDAO.getICalCalendar(token, addTasks);
			response.setContentType("text/calendar");
			CalendarOutputter output = new CalendarOutputter();
			output.output(calendar, response.getOutputStream());
			response.flushBuffer();
			return;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
	}

}
